import Vue from 'vue'
import Vuex from 'vuex'
import api from "../api/index"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //登录
    token:'',
    ldata:{},
    //商品
    shopList:[],
    addList:{},
  },
  mutations: {
    //登录
    logintoken(state,payload){
      state.token=payload
      window.localStorage.setItem("token",payload)
    },
    logindata(state,payload){
      state.ldata=payload
      window.localStorage.setItem("token",payload)
    },
    //商品
    SHOP_LIST(state,payload){
      state.shopList=payload
    },
    ADD_LIST(state,payload){
      state.addList=payload
    }

  },
  actions: {
    //登录
    Login(context,data){
      return new Promise((resolve,reject)=>{

        api.login(data).then(res=>{
        if(res.code===1){
          context.commit("logintoken",res.data.token)
        context.commit("logindata",res.data.data)
        resolve(res.data)
        }else{
          reject(res.message)
        }
      }).catch(err=>{
        reject(err)
      })
      
      })
    },

    //商品数据
    SHOP_LIST(context,data){
      api.shoplist(data).then(res=>{

        context.commit("SHOP_LIST",res.data.data)
      })
    },

    //加入数据
    ADD(context,data){
      api.shopaddlist(data).then(res=>{
       context.commit("ADD_LIST",res.data.data)
      })
    }
  },
  modules: {
  }
})
